﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Inspiration.Web.Controllers
{
    public class CodeNamesController : Controller
    {
        
        #region Http get for Index()
        //
        // GET: /Random/

        [HttpGet]
        public ActionResult Index()
        {
            System.Web.Security.MembershipUser currentUser = System.Web.Security.Membership.GetUser();
            Guid userId = (Guid)currentUser.ProviderUserKey;

            Inspiration.Web.Models.InspirationEntities entitiesRepository = new Models.InspirationEntities();
            IEnumerable<Inspiration.Web.Models.Idea> ideas = entitiesRepository.Ideas.Where(idea => idea.OwnerId == userId);

            return View(ideas);
        }
        #endregion Http get for Index()

        #region Http actions for Create()
        [HttpGet]
        public ActionResult Create()
        {
            // Display blank page
            return View();
        }

        [HttpPost]
        public ActionResult Create(FormCollection formValues)
        {
            // Get current user
            System.Web.Security.MembershipUser currentUser = System.Web.Security.Membership.GetUser();
            Guid userId = (Guid)currentUser.ProviderUserKey;

            // 
            Models.Idea newIdea = new Models.Idea();
            newIdea.Name = formValues["Name"];
            newIdea.OwnerName = currentUser.UserName;
            newIdea.OwnerId = userId;

            Inspiration.Web.Models.InspirationEntities entitiesRepository = new Models.InspirationEntities();
            entitiesRepository.AddToIdeas(newIdea);
            entitiesRepository.SaveChanges();

            // To return "postback" values use the line below
            //return View(newIdea);

            return View();
        }
        #endregion Http actions for Create()

        #region Http actions for Edit()
        [HttpGet]
        public ActionResult Edit(int id)
        {
            Inspiration.Web.Models.InspirationEntities entitiesRepository = new Models.InspirationEntities();
            Inspiration.Web.Models.Idea idea = entitiesRepository.Ideas.Single(i => i.IdeaId == id);
            return View(idea);
        }

        [HttpPost]
        public ActionResult Edit(int id, Inspiration.Web.Models.Idea idea, FormCollection formValues)
        {
            // Get current user
            System.Web.Security.MembershipUser currentUser = System.Web.Security.Membership.GetUser();
            Guid userId = (Guid)currentUser.ProviderUserKey;

            Inspiration.Web.Models.InspirationEntities entitiesRepository = new Models.InspirationEntities();
            Inspiration.Web.Models.Idea ideaModel = entitiesRepository.Ideas.Single(i => i.IdeaId == id);
            ideaModel.Name = idea.Name;
            ideaModel.InUsed = idea.InUsed;
            ideaModel.ApplicationName = idea.ApplicationName;
            UpdateModel(idea, formValues);

            // retrieving using (int id, FormCollection formValues)
            //ideaModel.IdeaId = id;
            //ideaModel.Name = formValues["Name"];
            //ideaModel.InUsed = bool.Parse(formValues["InUsed"]);
            //ideaModel.ApplicationName = formValues["ApplicationName"];
            
            entitiesRepository.SaveChanges();

            // display blank page
            return View("Saved");
        }
        #endregion Http actions for Edit()

        #region Http actions for Details()
        [HttpGet]
        public ActionResult Details(int id)
        {
            Inspiration.Web.Models.InspirationEntities entitiesRepository = new Models.InspirationEntities();
            Inspiration.Web.Models.Idea idea = entitiesRepository.Ideas.Single(i => i.IdeaId == id);
            return View(idea);
        }

        [HttpPost]
        public ActionResult Details(int id, Inspiration.Web.Models.Idea idea, FormCollection formValues)
        {
            // Get current user
            System.Web.Security.MembershipUser currentUser = System.Web.Security.Membership.GetUser();
            Guid userId = (Guid)currentUser.ProviderUserKey;

            //Inspiration.Web.Models.InspirationEntities entitiesRepository = new Models.InspirationEntities();
            //Inspiration.Web.Models.Idea ideaModel = entitiesRepository.Ideas.Single(i => i.IdeaId == id);
            //ideaModel.Name = idea.Name;
            //ideaModel.InUsed = idea.InUsed;
            //ideaModel.ApplicationName = idea.ApplicationName;
            //UpdateModel(idea, formValues);

            //// retrieving using (int id, FormCollection formValues)
            ////ideaModel.IdeaId = id;
            ////ideaModel.Name = formValues["Name"];
            ////ideaModel.InUsed = bool.Parse(formValues["InUsed"]);
            ////ideaModel.ApplicationName = formValues["ApplicationName"];

            //entitiesRepository.SaveChanges();

            // display blank page
            return View();
        }
        #endregion Http actions for Details()

        #region Http actions for Delete()
        [HttpGet]
        public ActionResult Delete(int id)
        {
            Inspiration.Web.Models.InspirationEntities entitiesRepository = new Models.InspirationEntities();
            Inspiration.Web.Models.Idea idea = entitiesRepository.Ideas.Single(i => i.IdeaId == id);
            return View(idea);
        }

        [HttpPost]
        public ActionResult Delete(int id, Inspiration.Web.Models.Idea idea, FormCollection formValues)
        {
            // Get current user
            System.Web.Security.MembershipUser currentUser = System.Web.Security.Membership.GetUser();
            Guid userId = (Guid)currentUser.ProviderUserKey;

            Inspiration.Web.Models.InspirationEntities entitiesRepository = new Models.InspirationEntities();
            Inspiration.Web.Models.Idea ideaModel = entitiesRepository.Ideas.Single(i => i.IdeaId == id);
            entitiesRepository.Ideas.DeleteObject(ideaModel);

            //ideaModel.Name = idea.Name;
            //ideaModel.InUsed = idea.InUsed;
            //ideaModel.ApplicationName = idea.ApplicationName;
            //UpdateModel(idea, formValues);

            //// retrieving using (int id, FormCollection formValues)
            ////ideaModel.IdeaId = id;
            ////ideaModel.Name = formValues["Name"];
            ////ideaModel.InUsed = bool.Parse(formValues["InUsed"]);
            ////ideaModel.ApplicationName = formValues["ApplicationName"];

            entitiesRepository.SaveChanges();

            // display blank page
            return View("Deleted");
        }
        #endregion Http actions for Details()
    }
}
